<?php

namespace App\Services\MakeAppleTreeRecords;

use Carbon\Carbon;
use App\Models\User;
use App\Models\AppleTree;
use App\Models\AppleTreesRecord;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class MakeAppleTreeRecords
{
	/**
	 * @var User
	 */
	protected $user;

	protected $tree;

	/**
	 * @var User
	 */

	public function __construct(User $user,AppleTree $tree)
	{
		$this->user = $user;
		$this->tree = $tree;
	}

	protected function buy()
	{
		DB::transaction(function () {

			$purchase = $this->user->appleTreeRecords()->create([
				'apple_tree_id' => $this->tree->id,
				'paid_at' => Carbon::now(), 
				'expired_at' => Carbon::now()->addDays(30),
				'money'=> $this->tree->consume_apples,
			]);

			account_apples($this->user)->minusAppleExchangeTrees($this->tree->consume_apples);
		});
	}

	public function apply()
	{

		$this->buy();
	}
}
